#include "stdafx.h"
#include "cef_chatgpt.h"
#include "module\cef\cef_global_methods.h"
using namespace ui;
using namespace std;

CefChatGPT::CefChatGPT()
{
	cef_control_ = nullptr;
}

ui::Control* CefChatGPT::CreateControl(const std::wstring& pstrClass)
{
	if (pstrClass == L"CefControl")
	{
		return new CefControl;
	}
	return NULL;
}

void CefChatGPT::DoInit()
{
	AttachBubbledEvent(ui::kEventClick, nbase::Bind(&CefChatGPT::OnClicked, this, std::placeholders::_1));
	cef_control_ = static_cast<CefControl*>(FindSubControl(L"cef_control"));
	cef_control_->AttachMenuCommand(nbase::Bind(&CefChatGPT::OnMenuCommand, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
	cef_control_->AttachLinkClick(nbase::Bind(&CefChatGPT::OnLinkClick, this, std::placeholders::_1));
	cef_control_->AttachBeforeNavigate(nbase::Bind(&CefChatGPT::OnBeforeNavigate, this, std::placeholders::_1, std::placeholders::_2));
	cef_control_->AttachLoadingStateChange(nbase::Bind(&CefChatGPT::OnLoadingStateChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
	cef_control_->AttachLoadEnd(nbase::Bind(&CefChatGPT::OnLoadEnd, this, std::placeholders::_1));
	cef_control_->AttachLoadError(nbase::Bind(&CefChatGPT::OnLoadError, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
	cef_control_->AttachAfterCreated(nbase::Bind(&CefChatGPT::OnAfterCreated, this, std::placeholders::_1));
	cef_control_->AttachBeforeCLose(nbase::Bind(&CefChatGPT::OnBeforeClose, this, std::placeholders::_1));
	cef_control_->AttachBeforeContextMenu(nbase::Bind(&CefChatGPT::OnBeforeContextMenu, this, std::placeholders::_1, std::placeholders::_2));
	std::wstring html_path = L"https://api.metachat.la/index.php/chat?token=" + nbase::UTF8ToUTF16(app_sdk::AppSDKInterface::GetInstance()->user_token);
	cef_control_->LoadURL(html_path);

	CefControl* cef_control_dev_ = dynamic_cast<CefControl*>(FindSubControl(L"cef_control_dev"));
	if (cef_control_dev_) cef_control_->AttachDevTools(cef_control_dev_);
}

bool CefChatGPT::OnClicked(ui::EventArgs* arg)
{
	std::wstring name = arg->pSender->GetName();
	if (name == L"btn_back")
	{
		cef_control_->GoBack();
	}
	else if (name == L"btn_forward")
	{
		cef_control_->GoForward();
	}
	else if (name == L"btn_refresh")
	{
		cef_control_->Refresh();
	}
	return false;
}

bool CefChatGPT::OnMenuCommand(CefRefPtr<CefContextMenuParams> params, int command_id, CefContextMenuHandler::EventFlags event_flags) {
	return false;
}

bool CefChatGPT::OnLinkClick(const std::wstring& url) {
	return true;
}

CefRequestHandler::ReturnValue CefChatGPT::OnBeforeNavigate(CefRefPtr<CefRequest> request, bool is_redirect) {
	return RV_CONTINUE;
}

void CefChatGPT::OnLoadingStateChange(bool isLoading, bool canGoBack, bool canGoForward) {
}

void CefChatGPT::OnLoadEnd(int httpStatusCode) {
}

void CefChatGPT::OnLoadError(CefLoadHandler::ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) {
}

void CefChatGPT::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
}

void CefChatGPT::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
}

void CefChatGPT::OnBeforeContextMenu(CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model) {
	model->Clear();
}